//统一支付
exports.unifiedPayment = function(req, res) {

	//获取restful参数
	var bodyParams = req.routeInfo.bodyParams;
	console.log('bodyParams-----------------')
	console.log(bodyParams)
	//支付类型
	var tradeType = 'JSAPI';
	
	if(bodyParams.trade_type){
		tradeType = 'NATIVE';
	}else{
		tradeType = 'JSAPI';
	}

	//最终结果
	var resultData = {};

	//任务流创建
	async.waterfall([

		//统一支付unifiedPayment
		function(cb) {

			var unifyUrl = "http://"+config.publicConfig.SERVER_IP+"/WCHATPAY/wpay.php";
//			var unifyUrl = "http://demo2.artmofang.com/WCHATPAY/wpay.php";
			var attach = {
				
				"apo_oid": bodyParams.apo_oid,
				"apo_pid": bodyParams.apo_pid,
				"o_no": bodyParams.o_no, //订单号
				"u_id": bodyParams.u_id, //用户ID
				"o_balance_pay": bodyParams.o_balance_pay, //余额支付部分
				"total_fee": bodyParams.total_fee, //微信实际支付部分
				"sell_id": bodyParams.sell_id || 0, //销售ID
				"p_id": bodyParams.p_id || 0, //产品id
//				"productName": bodyParams.productName || '', //产品名称
			};
			var attachStr = JSON.stringify(attach);
			console.log('attachStr----------------------')
			console.log(attachStr)
			//			var unifyUrl = "http://192.168.0.8/WCHATPAY/wpay.php";

			R.SEND_HTTP(
				req, {
					url: unifyUrl,
					method: 'POST',
					data: {
						"body": bodyParams.body,
						"total_fee": bodyParams.total_fee,
						"out_trade_no": bodyParams.outTradeNo,
						"attach": attachStr,
						"open_id": bodyParams.openId,
						"product_id": bodyParams.body,
						"trade_type": tradeType //支付类型
					}
				},
				function(err, data) {

					console.log('获取的统一支付参数')
					console.log(data)

					if(err) {
						return RES.response(res, false, "统一支付获取失败");
					}

					resultData.unify = data;

					cb();

				}
			);

		},

		//签名
		function(cb) {

			var signatureUrl = "http://"+config.publicConfig.SERVER_IP+"/WCHATPAY/wpay.php";
//			var signatureUrl = "http://demo2.artmofang.com/WCHATPAY/wpay.php";
			//			var signatureUrl = "http://192.168.0.8/WCHATPAY/wpay.php";

			R.SEND_HTTP(
				req, {
					url: signatureUrl,
					method: 'POST',
					data: {
						"action": "jssdk",
						"prepay_id": resultData.unify.prepay_id
					}
				},
				function(err, data) {

					console.log('获取的签名参数')
					console.log(data)

					if(err) {
						return RES.response(res, false, "签名获取失败");
					}

					resultData = data;

					cb();

				}
			);

		}

	], function(err, result) {

		//返回最终拼接结果
		RES.response(res, true, resultData);

	});

}